﻿using MusicZone.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace MusicZone
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class Spoke1 : MusicZone.Common.LayoutAwarePage
    {
        public Spoke1()
        {
            this.InitializeComponent();
            TopAppBar.AppBarHome.Click += AppBarHome_Click;
            TopAppBar.AppBarArtists.Click += AppBarArtists_Click;
        }

        void AppBarArtists_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Spoke2), "AllGroups");
        }

        void AppBarHome_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(HubPage), "AllGroups");
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            var Spoke1DataGroups = Spoke1DataSource.GetGroups((String)navigationParameter);
            this.DefaultViewModel["Groups"] = Spoke1DataGroups;
        }

        private void Add_Clicked(object sender, RoutedEventArgs e)
        {
            PlayListPopup();
            BotAppBar.IsOpen = false;
            TopBar.IsOpen = false;
        }

        public static Popup settingsPopup = null;
        void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {
                settingsPopup.IsOpen = false;
            }
        }

        void settingsPopup_Closed(object sender, object e)
        {
            Window.Current.Activated -= Current_Activated;
        }

        private void PlayListPopup()
        {
            settingsPopup = new Popup();
            Rect windowsBounds = Window.Current.Bounds;
            settingsPopup.Closed += settingsPopup_Closed;
            Window.Current.Activated += Current_Activated;

            settingsPopup.IsLightDismissEnabled = true;
            settingsPopup.Height = windowsBounds.Height;
            PlayList filter = new PlayList();

            var items = PlayListDataSource.GetGroups("AllGroups");
            foreach (var item in items)
            {
              filter.itemListView.ItemsSource=  item.TopItems;
            }          

            filter.Height = windowsBounds.Height;

            settingsPopup.Child = filter;
            settingsPopup.SetValue(Canvas.LeftProperty, windowsBounds.Width - 750);
            settingsPopup.SetValue(Canvas.TopProperty, 0);
            settingsPopup.IsOpen = true;
            settingsPopup.ChildTransitions = new TransitionCollection();
            settingsPopup.ChildTransitions.Add(new EdgeUIThemeTransition()
            {
                Edge = (SettingsPane.Edge == SettingsEdgeLocation.Right) ?
                       EdgeTransitionLocation.Right :
                       EdgeTransitionLocation.Left
            });
        }

       
    }
}
